package com.wanmao.test.shebei;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.wanmao.test.CheckActivity;
import com.wanmao.test.util.DialogUtils;
import com.wanmao.test.view.progressView;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

import static android.view.View.VISIBLE;



public class GetSBLXAsynTask extends AsyncTask<String, Void, Void> {
    private SQLdm s;
    private CheckActivity mContext = null;
    private static List<Item> list;
    private ListView mListView;
    private progressView mprogressView;
    private static MyAdapter adapter;
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;

    public GetSBLXAsynTask(CheckActivity context, ListView lv, progressView pv) {
        this.mContext = context;
        this.mListView = lv;
        this.mprogressView = pv;
    }


    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        DialogUtils.startProgressDialog(mContext,"正在加载");

        list = new ArrayList<>();

    }


    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        setAdapter();
        saveLocalList();

        DialogUtils.stopProgressDialog();
    }

    @Override
    protected Void doInBackground(String... strings) {
        getLocalList();
        if (list.size() < 1) {
            s = new SQLdm(mContext);
            initListData();
        }
        return null;
    }

    public static List<Item> getSBList() {
        return list;
    }

    private void initListData() {
        int levelCount = 0;
        SQLiteDatabase db = s.openDatabase();
        List<ExcelInfo> infoList = s.getMainInfo(db);
        if (infoList.size() > 0) {
            for (int i = 0; i < infoList.size(); i++) {
               list.add(new Item(i, 0, levelCount, false, infoList.get(i).getSBLX()));
                getChildNode(infoList.get(i), i, levelCount);
            }
        }
    }

    private void getChildNode(ExcelInfo info, int pid, int count) {
        int levelCount = count + 1;
        SQLiteDatabase db = s.openDatabase();
        List<ExcelInfo> infoList = s.getSubInfo(db, info.getOBJ_ID());
        if (infoList.size() > 0) {
            for (int i = 0; i < infoList.size(); i++) {
                list.add(new Item((pid + 1) * 3000 + i, pid, levelCount, false, infoList.get(i).getSBLX()));
                getChildNode(infoList.get(i), pid * 3000 + i, levelCount);
            }
        }
    }


    private void setAdapter() {
        adapter = new MyAdapter(list, mContext);

        adapter.setOnInnerItemClickListener(new SBLXTreeAdapter.OnInnerItemClickListener<Item>() {
            @Override
            public void onClick(Item node) {
             mContext.close(node.name);

            }
        });
        adapter.setOnInnerItemLongClickListener(new SBLXTreeAdapter.OnInnerItemLongClickListener<Item>() {
            @Override
            public void onLongClick(Item node) {
            }
        });

        mListView.setAdapter(adapter);
    }

    private void getLocalList() {
        SharedPreferences mSharedPreference1 = mContext.getSharedPreferences("ingoreList", mContext.MODE_PRIVATE);
        list.clear();
        int size = mSharedPreference1.getInt("Status_size", 0);
        if (size == 0) {
            return;
        }
        ;
        for (int i = 0; i < size; i++) {
            String itemString = mSharedPreference1.getString("Status_" + i, null);
            if (!(itemString.equals("") && itemString == null)) {
                byte[] base64SItem = Base64.decode(itemString, Base64.DEFAULT);
                ByteArrayInputStream bais = new ByteArrayInputStream(base64SItem);
                try {
                    ObjectInputStream ois = new ObjectInputStream(bais);
                    Item item = (Item) ois.readObject();
                    list.add(item);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


        }
        ;
    }

    public boolean saveLocalList() {
        SharedPreferences sp = mContext.getSharedPreferences("ingoreList", mContext.MODE_PRIVATE);
        SharedPreferences.Editor mEditSave = sp.edit();
        mEditSave.putInt("Status_size", list.size());
        for (int i = 0; i < list.size(); i++) {
            mEditSave.remove("Status_" + i);
            ByteArrayOutputStream baos = new ByteArrayOutputStream(3000);
            ObjectOutputStream oos = null;
            try {
                oos = new ObjectOutputStream(baos);
                oos.writeObject(list.get(i));
            } catch (IOException e) {
                e.printStackTrace();
            }

            String base64SItem = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
            mEditSave.putString("Status_" + i, base64SItem);
        }
        return mEditSave.commit();
    }

}
